#include "StdAfx.h"
#include "MainMenu.h"
#include "GameController.h"
#include "AnimatedImage.h"
#include "RenderingManager.h"
#include "InputController.h"
#include "MeshStorage.h"


///////////////////////////////////////////////////////////////////////////////

MainMenu::MainMenu(IGameController& gameController,
                   CInputController& inputController,
                   IRenderingManager& renderingManager,
                   IMeshStorage& meshStorage)
      : m_gameController(gameController),
      m_inputController(inputController),
      m_renderingManager(renderingManager),
      m_mainMenuImage(meshStorage.getMainMenuScreen()),
      m_selectionHighlight(meshStorage.getSelectionHighlight()),
      m_activeOption(0),
      m_selectionChanged(false),
      m_timeUntilNextOptionChange(0.1f)
{
   m_menuX = 0.2f;
   m_menuY.push_back(0.1f);
   m_menuY.push_back(0.42f);
   m_menuY.push_back(0.75f);

   m_selectionHighlight.setPosition(m_menuX, m_menuY[m_activeOption]);
}

///////////////////////////////////////////////////////////////////////////////

void MainMenu::update(float timeElapsed)
{
   if (m_selectionChanged)
   {
      m_selectionHighlight.setPosition(m_menuX, m_menuY[m_activeOption]);
      m_selectionChanged = false;
   }
}

///////////////////////////////////////////////////////////////////////////////

void MainMenu::activate()
{
   m_renderingManager.startRendering(m_mainMenuImage);
   m_renderingManager.startRendering(m_selectionHighlight);
   m_inputController.setProcessor(*this);
}

///////////////////////////////////////////////////////////////////////////////

void MainMenu::deactivate()
{
   m_renderingManager.stopRendering(m_mainMenuImage);
   m_renderingManager.stopRendering(m_selectionHighlight);
   m_inputController.resetProcessor();
}

///////////////////////////////////////////////////////////////////////////////

void MainMenu::processInput(UCHAR* keyBuffer, float timeElapsed)
{
   // selecting an option is not a subject to the 'key-is-hit-multiple-times-during-a-sec' problem :)
   processSelection(keyBuffer);

   // counteract the effect of a button being constantly kept,
   // so that we can actually see what is being selected
   m_timeUntilNextOptionChange -= timeElapsed;
   if (m_timeUntilNextOptionChange > 0)
   {
      return;
   }

   processBrowsing(keyBuffer);

   if (m_selectionChanged)
   {
      m_timeUntilNextOptionChange = 0.5;
   }
}

///////////////////////////////////////////////////////////////////////////////

void MainMenu::processBrowsing(UCHAR* keyBuffer)
{
   // process the user's choice
   if (IS_PRESSED(keyBuffer, VK_UP))     {m_activeOption--; m_selectionChanged = true;}
   if (IS_PRESSED(keyBuffer, VK_DOWN))   {m_activeOption++; m_selectionChanged = true;}

   if (m_activeOption < 0)
   {
      m_activeOption = m_menuY.size() - 1;
   }
   else if ((UINT)m_activeOption >= m_menuY.size())
   {
      m_activeOption = 0;
   }
}

///////////////////////////////////////////////////////////////////////////////

void MainMenu::processSelection(UCHAR* keyBuffer)
{
   if (IS_PRESSED(keyBuffer, VK_RETURN))
   {
      switch(m_activeOption)
      {
         case MO_EASY_TRACK:
         {
            m_gameController.startRace("easy");
            break;
         }

         case MO_DIFFICULT_TRACK:
         {
            m_gameController.startRace("difficult");
            break;
         }

         case MO_EXIT:
         {
            m_gameController.closeGame();
            break;
         }

         default:
         {
            break;
         }
      }
   }
}

///////////////////////////////////////////////////////////////////////////////
